package com.d2.common.excel;


import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

public class CustomSheetWriteHandler implements SheetWriteHandler {
    /**
     * 列宽集合
     */
    private List<Integer> columnWidths;

    /**
     * 构造
     */
    public CustomSheetWriteHandler(List<Integer> columnWidths) {
        this.columnWidths = columnWidths;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder,
                                  WriteSheetHolder writeSheetHolder) {
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder,
                                 WriteSheetHolder writeSheetHolder) {
        if (CollectionUtils.isNotEmpty(columnWidths)) {
            for (int i = 0; i < columnWidths.size(); i++) {
                writeSheetHolder.getSheet().setColumnWidth(i, columnWidths.get(i));
            }
        }
    }
}


